<?php

namespace App\Libs;

use GuzzleHttp\Client;
use Hyperf\Di\Annotation\Inject;

class GuzzleRequest
{
    #[Inject] private Client $client;


    public function get (string $url, array $data = [],array $options = [])
    {
        if($data){
            $options['query']=$data;
        }
        return $this->curl('get', $url, $options);
    }

    public function post (string $url, array $data = [], array $options = [])
    {
        $options['form_params'] = $data;
        return $this->curl('post', $url, $options);
    }


    private function curl (string $method, string $url, array $options = [])
    {
        try {
            $options['timeout'] = 30;
            $res = $this->client->request($method, $url, $options);
            return json_decode($res->getBody()->getContents(),true);
        } catch (\Exception $exception) {
            return json_decode($exception,true);
        }
    }





}